/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.oper;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * <p>Project: app2022 - Bit1
 * <p>Powered by webrx On 2022-01-06 15:55:02
 *
* @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Bit2 {
    public static void main(String[] args) {
        System.out.println(12 & 5);
        System.out.println(Integer.toBinaryString(12));
        //1100
        //0101   1100 & 0101  = 100
        System.out.println(Integer.toBinaryString(5));
        System.out.println(0b100);

        System.out.println(3 >> 1);
        System.out.println(3 >> 1 << 3); //8
        System.out.println(0b1000);

        System.out.println(Integer.toBinaryString(3));
        System.out.println(0b11111111111111111111111111111101);

        System.out.println("-".repeat(50));
        System.out.println(iptonum("58.83.160.156"));
        System.out.println(numtoip(978559132));
        System.out.println("-".repeat(50));

        //58.83.160.156 ping 58.83.160.156
        //ping mi.com
        long ip = 0;
        ip += 58 * Math.pow(256, 3);
        ip += 83 * Math.pow(256, 2);
        ip += 160 * Math.pow(256, 1);
        ip += 156 * Math.pow(256, 0);
        System.out.println(ip);

    }

    static String numtoip(long ip) {
        byte[] bys = new byte[]{(byte) (ip >> 24), (byte) (ip >> 16), (byte) (ip >> 8), (byte) ip};
        String ips = "";
        try {
            ips = InetAddress.getByAddress(bys).getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return ips;
    }

    static long iptonum(String ipv4) {
        long ip = 0;
        var is = ipv4.split("\\.");
        long ipn = 0;
        for (int i = 0; i < is.length; i++) {
            ip += Integer.parseInt(is[i]) * Math.pow(256, is.length - i - 1);
        }
        return ip;
    }
}
